<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{id}}.{{title}}</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        :root {
            --primary-color: #3498db;
            --secondary-color: #2ecc71;
            --background-color: #f4f4f4;
            --text-color: #333;
            --navbar-bg: rgba(51, 51, 51, 0.9);
            --border-color: #e5e7eb;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            line-height: 1.6;
            color: var(--text-color);
            background-color: var(--background-color);
            height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .navbar {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            height: 60px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            background-color: var(--navbar-bg);
            padding: 7px 14px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
            z-index: 1000;
        }

        .navbar-left, .navbar-right {
            display: flex;
            align-items: center;
        }

        .navbar a {
            color: white;
            text-decoration: none;
            font-size: 17px;
            margin: 0 13px;
            padding: 10px 15px;
            border-radius: 30px;
            transition: background-color 0.3s ease, transform 0.3s ease;
        }

        .navbar a:hover {
            background-color: var(--primary-color);
            transform: scale(1.05);
        }

        .login {
            background-color: var(--secondary-color);
        }

        .main-content {
            display: flex;
            flex: 1;
            overflow: hidden;
            margin-top: 60px;
        }

        .left-desc, .right-code {
            width: 50%;
            height: 100%;
            overflow-y: auto;
        }

        .left-desc {
            padding: 1rem;
            background-color: white;
            border-right: 1px solid var(--border-color);
            font-size: 1.125rem; 
            line-height: 1.5;    
            height: calc(100vh - 60px); 
            overflow-y: auto; 
        }

        .left-desc h2 {
            font-size: 1.5rem; 
            font-weight: bold;
            margin-bottom: 0.75rem;
        }

        .right-code {
            display: flex;
            flex-direction: column;
        }

        .editor-header {
            padding: 0.75rem 1rem;
            background-color: white;
            border-bottom: 1px solid var(--border-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .editor-title {
            font-weight: 600;
            cursor: pointer;
            padding: 0.25rem 0.5rem;
            border-radius: 1.5rem;
            transition: background-color 0.3s ease, transform 0.3s ease;
        }

        .editor-title:hover {
            background-color: #e8f0fe;
            transform: scale(1.05);
        }
        
        .editor-controls {
            display: flex;
            align-items: center;
            gap: 1rem;
        }

        .language-select {
            padding: 0.25rem 0.5rem;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            background-color: white;
        }

        .mode-indicator {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            color: #6b7280;
        }

        #editor {
            width: 100%;
            height: 800px;
        }

        .result-container {
            border-top: 1px solid var(--border-color);
            padding: 1rem;
            background-color:#f9f9f9;
            display: flex;
            justify-content: space-between;
            align-items: flex-start;
            height: 170px;
        }

        .result {
            flex: 1;
            overflow-y: auto;
            max-height: 150px;
            margin-right: 1rem;
        }

        .btn-submit {
            padding: 0.75rem 1.5rem;
            font-size: 1rem;
            background-color: var(--secondary-color);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
            align-self: flex-start;
        }

        .btn-submit:hover {
            background-color: #27ae60;
        }
    </style>
</head>
<body>
    <nav class="navbar">
        <div class="navbar-left">
            <a href="/">首页</a>
            <a href="/all_questions">题库</a>
            <a href="#">竞赛</a>
            <a href="#">讨论</a>
            <a href="#">求职</a>
        </div>
        <div class="navbar-right">
            <a href="#" class="login">登录</a>
        </div>
    </nav>

    <div class="main-content">
        <div class="left-desc">
            <h2><span id="id">{{id}}</span>. {{title}} ({{star}})</h2>
            <pre>{{desc}}</pre>
        </div>
        <div class="right-code">
            <div class="editor-header">
                <div class="editor-title">代码</div>
                <div class="editor-controls">
                    <select class="language-select">
                        <option value="cpp">C++</option>
                        <option value="java">Java</option>
                        <option value="python">Python</option>
                    </select>
                    <div class="mode-indicator">
                        <span>🔒</span>
                        <span>智能模式</span>
                    </div>
                </div>
            </div>
            <div id="editor"></div>
        </div>
    </div>

    <div class="result-container">
        <div class="result"></div>
        <button class="btn-submit" onclick="submit()">提交代码</button>
    </div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.30.1/min/vs/loader.min.js"></script>
    <script>
        require.config({ paths: { 'vs': 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.30.1/min/vs' }});
        require(['vs/editor/editor.main'], function() {
            monaco.editor.defineTheme('lightTheme', {
                base: 'vs',
                inherit: true,
                rules: [],
                colors: {
                    'editor.background': '#f5f5f5',
                }
            });

            var preCode = `{{pre_code}}`;  // 这里使用模板变量获取预设代码

            window.editor = monaco.editor.create(document.getElementById('editor'), {
                value: preCode,  // 直接设置编辑器的内容
                language: 'cpp',
                theme: 'lightTheme',
                automaticLayout: true,
                minimap: { enabled: false },
                fontSize: 14,
                lineNumbers: 'on',
                roundedSelection: false,
                scrollBeyondLastLine: false,
                readOnly: false,
                tabSize: 4,
                quickSuggestions: true,
                suggestOnTriggerCharacters: true,
                acceptSuggestionOnEnter: "on",
                wordBasedSuggestions: true,
                parameterHints: {
                    enabled: true
                },
                suggestSelection: "first",
                codeLens: true,
                folding: true,
                contextmenu: true,
                showUnused: true,
                links: true,
                formatOnType: true,
                formatOnPaste: true
            });

            // 添加C++基本语法提示
            monaco.languages.registerCompletionItemProvider('cpp', {
                provideCompletionItems: function(model, position) {
                    var suggestions = [
                        {
                            label: 'cout',
                            kind: monaco.languages.CompletionItemKind.Function,
                            insertText: 'cout << ${1:} << endl;',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            documentation: '输出到控制台'
                        },
                        {
                            label: '#include',
                            kind: monaco.languages.CompletionItemKind.Keyword,
                            insertText: '#include <${1:iostream}>',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            documentation: '包含头文件'
                        },
                        {
                            label: 'for',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'for (int ${1:i} = 0; ${1:i} < ${2:count}; ${1:i}++) {\n\t${3}\n}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            documentation: 'For 循环'
                        },
                        {
                            label: 'while',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'while (${1:condition}) {\n\t${2}\n}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            documentation: 'While 循环'
                        },
                        {
                            label: 'if',
                            kind: monaco.languages.CompletionItemKind.Snippet,
                            insertText: 'if (${1:condition}) {\n\t${2}\n}',
                            insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
                            documentation: 'If 语句'
                        }
                    ];
                    return { suggestions: suggestions };
                }
            });

            window.addEventListener('resize', function() {
                editor.layout();
            });
        });

        $('.language-select').on('change', function() {
            var language = $(this).val();
            monaco.editor.setModelLanguage(window.editor.getModel(), language);
        });

        function submit() {
            var code = window.editor.getValue();
            var id = $("#id").text();
            var judge_url = "/judge/" + id;
            
            $.ajax({
                method: 'POST',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ""
                }),
                success: function(data) {
                    show_result(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    show_result({
                        status: -1,
                        reason: "提交失败: " + textStatus,
                        stdout: "",
                        stderr: errorThrown
                    });
                }
            });
        }

        function show_result(data) {
            var result_div = $(".result");
            result_div.empty();
            
            var status = data.status;
            var reason = data.reason;

            result_div.append($("<h3>").text("执行结果"));
            result_div.append($("<p>").text(reason));

            if (status == 0) {
                var stdout = data.stdout;
                var stderr = data.stderr;

                if (stdout) {
                    result_div.append($("<h4>").text("标准输出"));
                    result_div.append($("<pre>").text(stdout));
                }
                
                if (stderr) {
                    result_div.append($("<h4>").text("标准错误"));
                    result_div.append($("<pre>").text(stderr));
                }
            }
        }
    </script>
</body>
</html>